﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chip8.Core.Processor
{
	internal class Stack
	{
		public Stack(ICPU cpu)
		{
			_cpu = cpu;
		}

		public int SP { get; set; }

		public void Reset()
		{
			Array.Clear(_stack, 0, Chip8.CPUStackSize);
			SP = 0;
		}

		private ushort[] _stack = new ushort[Chip8.CPUStackSize];

		public void Push(ushort value)
		{
			if (SP >= Chip8.CPUStackSize)
			{
				throw new StackOverflowException(_cpu.PC);
			}

			_stack[SP++] = value;
		}

		public ushort Pop()
		{
			if (SP == 0)
			{
				throw new StackUnderflowException(_cpu.PC);
			}

			return _stack[--SP];
		}

		private readonly ICPU _cpu;
	}
}
